<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

Class Line_service_model extends CI_Model {

    private $table = "line_service";
    private $tb = "line_equip";

    function __construct() {
        parent::__construct();
    }

    function insert($data = array()) {
        if ($data != '') {
            $val = array(
                'sdp_id' => $data['sdp_id'],
                'CH' => $data['CH'],
                'ls_type' => $data['ls_type'],
                'ls_commend' => $data['ls_commend'],
                'distance' => $data['distance'],
                'cm_id' => $data['cm_id'],
            );

            $is = ($this->db->insert($this->table, $val) ? $this->db->insert_id() : FALSE);
            if($is){
                $array = array('sdp_id' => $val['sdp_id'], 'CH' => $val['CH'], 'cm_id' => $val['cm_id']);
                $query = $this->db->where($array)->get($this->table)->result();
                     foreach ($query as $rs) {
                         $param = $rs->ls_id;
                     }
                     $rs = ($this->set_up($param)) ? TRUE : FALSE;
                     return $rs;
            }
        }
    }
    
      function set_up($param) {
        if ($param != '') {
            $stat = array(
                'ls_id' => $param,
                'line_eq_stat_id' => '0'
            );
            $str = ($this->db->insert($this->tb, $stat)) ? TRUE : FALSE;

            return $str;
        }
    }

    function update($data = array()) {
        if ($data != '') {
            $val = array(
                'CH' => $data['CH'],
                'ls_type' => $data['ls_type'],
                'ls_commend' => $data['ls_commend'],
                'distance' => $data['distance'],
                'cm_id' => $data['cm_id'],
            );
            $is = ($this->db->where('ls_id', $data['ls_id'])->update($this->table, $val)) ? TRUE : FALSE;
            return $is;
        }
    }

    function delete($data) {
        ($this->db->where('ls_id', $data)->delete($this->table)) ? $is = TRUE : $is = FALSE;
        return $is;
    }

    function select_by_sdpid($sdp_id) {
        $rs = $this->db->where('sdp_id', $sdp_id)->get("vw_".$this->table)->result();
        return $rs;
    }

    function select_by_filter($id) {
        $rs = $this->db->where('ls_id', $id)->get($this->table)->row_array();
        return $rs;
    }
    
    function select_by_view(){
        $rs = $this->db->get("vw_".$this->tb)->result();
        return $rs;
    }
    
    function update_equip($data=array()){
        if($data != ''){
            $val=array(
                'cm_code'=>$data['cm_code'],
                'cm_lineid'=>$data['cm_lineid']
            );
            $status = array(
                'line_eq_stat_id'=>'1',
                'line_eq_date'=>$data['line_eq_date']
            );
            $is = ($this->db->where($val)->update("vw_".$this->tb,$status)) ? TRUE : FALSE;
            return $is;
        }
    }
    
    function check_install_line($data=array()){
        $rs = $this->db->where($data)->get("vw_".$this->tb)->result();
        return $rs;
        
    }

}
